package com.fenqing.leetcode.code.t300_400;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.*;

public class Topic350 extends Topic {
    /**
     * 构造器
     *
     * @param input1 输入
     * @param input2 输入
     */
    public Topic350(String input1, String input2) {
        super(input1, input2);
    }

    @Execute
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> result = new ArrayList<>();
        Map<Integer, Integer> hash = new HashMap<>();
        for (int i : nums1) {
            Integer orDefault = hash.getOrDefault(i, 0);
            hash.put(i, orDefault + 1);
        }
        for (int i : nums2) {
            Integer integer = hash.get(i);
            if (integer != null && integer > 0) {
                result.add(i);
                hash.put(i, integer - 1);
            }
        }
        return result.stream().mapToInt(Integer::valueOf).toArray();
    }

    public static void main(String[] args) {
        Topic350 topic350 = new Topic350("""
                [1,2,2,1]
                """, """
                [2,2]
                """);
        int[] run = topic350.run();
        System.out.println(Arrays.toString(run));
    }
}
